/**
 *   This file is part of WiFiStorer.

    WiFiStorer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    WiFiStorer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with WiFiStorer.  If not, see <http://www.gnu.org/licenses/>.
    
    @author Ontic Soluciones
    @license GNU GPLv3
    @copyright 2010 Ontic Soluciones
    @link http://www.ontic.es
    @contact android@ontic.es
 */


package com.Storer;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;


/**
 * Crea una capa con los puntos de la wifi cuya mac y tabla se pasa como parámetro.
 * @author Ontic Soluciones
 *
 */
class MiOverlay extends Overlay 
{
    private Paint paint;
    private SQLiteDatabase DB;
    private DataBaseHelper db;
    private String wifi;
    private String tabla;

    /**
     * Constructor de la clase, crea una instancia de MiOverlay
     * @param DB La Base de datos sobre la que trabajamos
     * @param context El contexto de la actividad que lo ha llamado, para leer la BDD
     * @param wifi La Mac de la wifi a pintar
     * @param tabla La tabla de donde sacar los datos
     */
	public MiOverlay(SQLiteDatabase DB, Context context, String wifi, String tabla)
	{
		super();
		this.DB = DB;
		db = new DataBaseHelper(context);
        DB =  db.getReadableDatabase();
        this.wifi=wifi;
        this.tabla = tabla;
        paint = new Paint();
        paint.setARGB(255, 255, 0, 0);
        
	}

	/**
	 * Pinta círculos rojos según la coordenada de la wifi seleccionada en la base de datos
	 */
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);
    
        String select = "select lat, lng, pow from "+tabla+" WHERE bssid = '"+wifi+"'";
        Cursor c = DB.rawQuery(select, null);
        c.moveToFirst();
        
        for(int i = 0; i<c.getCount(); i++)
        {
        	int pot = c.getInt(2);
        	//paint.setAlpha(255 - (int)(pot*2.5));
        	paint.setColor(((-1)*((pot-20) *208893*2) -65536));
        	
			Point myScreenCoords = new Point() ;
			mapView.getProjection().toPixels(new GeoPoint(c.getInt(0),c.getInt(1)), myScreenCoords);
			canvas.drawOval(new RectF(myScreenCoords.x - 5, myScreenCoords.y + 5, 
				myScreenCoords.x + 5, myScreenCoords.y - 5), paint);
		
			c.moveToNext();
        }

		return shadow;       
     }
}

          